
namespace Behemoth.Web.UI {

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text;

    using System.IO;

    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

	public class FileSystemDataSourceControl : HierarchicalDataSourceControl {

    private FileSystemDataSourceView _view = null;

    public FileSystemDataSourceControl() : base() { }

		protected override HierarchicalDataSourceView GetHierarchicalView( string viewPath ) {
			if (null == this._view)
				this._view = new FileSystemDataSourceView( viewPath );
			return this._view;
		}

		protected override ControlCollection CreateControlCollection() {
			return new ControlCollection( this );
		}

	} // class

	public class FileSystemDataSourceView : HierarchicalDataSourceView {

		public FileSystemDataSourceView( string viewPath ) { }

		public override IHierarchicalEnumerable Select() {
			HttpRequest currentRequest = HttpContext.Current.Request;
			string rootPath = HttpContext.Current.Request.MapPath( currentRequest.ApplicationPath );
			DirectoryInfo rootDirectory = new DirectoryInfo( rootPath );
			FileSystemHierarchicalEnumerable fshe = new FileSystemHierarchicalEnumerable();
			foreach (FileSystemInfo fsi in rootDirectory.GetFileSystemInfos())
				fshe.Add( new FileSystemHierarchyData( fsi ) );
			return fshe;
		}

	} // class

	public class FileSystemHierarchicalEnumerable : ArrayList, IHierarchicalEnumerable {

		public FileSystemHierarchicalEnumerable() : base() { }

		public IHierarchyData GetHierarchyData( object enumeratedItem ) {
			return enumeratedItem as IHierarchyData;
		}

	}

	public class FileSystemHierarchyData : IHierarchyData {

		private FileSystemInfo _fileSystemObject = null;

		public FileSystemHierarchyData( FileSystemInfo obj ) {
			this._fileSystemObject = obj;
		}

		public override string ToString() {
			return this._fileSystemObject.Name;
		}

		public bool HasChildren {
			get {
				if (typeof( DirectoryInfo ) == this._fileSystemObject.GetType()) {
					DirectoryInfo temp = (DirectoryInfo)_fileSystemObject;
					return (temp.GetFileSystemInfos().Length > 0);
				} else
					return false;
			}
		}

		public string Path {
			get { return this._fileSystemObject.ToString(); }
		}

		public object Item {
			get { return this._fileSystemObject; }
		}

		public string Type {
			get { return "FileSystemData"; }
		}

		public IHierarchicalEnumerable GetChildren() {
			FileSystemHierarchicalEnumerable children = new FileSystemHierarchicalEnumerable();
			if (typeof( DirectoryInfo ) == this._fileSystemObject.GetType()) {
				DirectoryInfo temp = (DirectoryInfo)this._fileSystemObject;
				foreach (FileSystemInfo fsi in temp.GetFileSystemInfos())
					children.Add( new FileSystemHierarchyData( fsi ) );
				return children;
			}
			return null;
		}

		public IHierarchyData GetParent() {
			IHierarchyData parent = null;
			if (typeof( DirectoryInfo ) == this._fileSystemObject.GetType()) {
				DirectoryInfo temp = (DirectoryInfo)this._fileSystemObject;
				parent = new FileSystemHierarchyData( temp.Parent );
			} else if (typeof( FileInfo ) == _fileSystemObject.GetType()) {
				FileInfo temp = (FileInfo)_fileSystemObject;
				parent = new FileSystemHierarchyData( temp.Directory );
			}
			return parent;
		}

	} // class

} // namespace
